package com.practice.tkmybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan(basePackages = {"com.practice.tkmybatis.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryBean")
public class DataSourceConfig {

    /**
     * 主库属性
     */
    @Value("${master.datasource.driver-class-name}")
    private String masterDriverClassName;

    @Value("${master.datasource.url}")
    private String masterUrl;

    @Value("${master.datasource.username}")
    private String masterUserName;

    @Value("${master.datasource.password}")
    private String masterPassword;

    /**
     * 从库属性
     */
    @Value("${slave.datasource.driver-class-name}")
    private String slaveDriverClassName;

    @Value("${slave.datasource.url}")
    private String slaveUrl;

    @Value("${slave.datasource.username}")
    private String slaveUserName;

    @Value("${slave.datasource.password}")
    private String slavePassword;

    /**
     * 主库配置
     *
     * @return
     */
    @Bean
    public DruidDataSource masterDruidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(masterDriverClassName);
        druidDataSource.setUrl(masterUrl);
        druidDataSource.setUsername(masterUserName);
        druidDataSource.setPassword(masterPassword);
        return druidDataSource;
    }

    /**
     * 从库配置
     *
     * @return
     */
    @Bean
    public DruidDataSource slaveDruidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(slaveDriverClassName);
        druidDataSource.setUrl(slaveUrl);
        druidDataSource.setUsername(slaveUserName);
        druidDataSource.setPassword(slavePassword);
        return druidDataSource;
    }

    /**
     * 配置动态数据类
     *
     * @param masterDruidDataSource 主数据源
     * @param slaveDruidDataSource  从数据源
     * @return 动态数据源
     */
    public MyDataSource myDataSource(DruidDataSource masterDruidDataSource, DruidDataSource slaveDruidDataSource) {
        MyDataSource myDataSource = new MyDataSource();
        myDataSource.setDefaultTargetDataSource(masterDruidDataSource);
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("master", masterDruidDataSource);
        dataSourceMap.put("slave", slaveDruidDataSource);
        myDataSource.setTargetDataSources(dataSourceMap);
        return myDataSource;
    }

    public SqlSessionFactoryBean sqlSessionFactoryBean(MyDataSource myDataSource) {
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(myDataSource);
        return sqlSessionFactoryBean;
    }

    public static void main(String[] args) {
        MyThreadLocal.setDataSource("master");
        // 执行master库的逻辑
        MyThreadLocal.setDataSource("slave");
        // 执行slave库的逻辑
    }
}
